package com.cssy.yd.basic.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.yd.basic.entity.BasicBomDetailsDO;
import com.cssy.yd.basic.entity.BasicBomInfoDO;
import com.cssy.yd.basic.entity.BasicBomRawMaterialDO;
import com.cssy.yd.basic.entity.BasicMaterialsDO;
import com.cssy.yd.basic.service.IBasicBomInfoService;
import com.cssy.yd.basic.service.IBasicBomSubstituteService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchResult;
import com.plat.util.ExcelUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Controller
@RequestMapping("yd/basic/bomInfo")
public class BasicBomInfoController extends BaseController<BasicBomInfoDO>{
	private static final String ITEM = "/yd/basic/bomInfo/ItemManager";// 选择物料
	private static final String MANAGER = "/yd/basic/bomInfo/BomInfoManager";//列表页面
	private static final String EIDT = "/yd/basic/bomInfo/BomInfoOperation";//添加、修改、显示页面
	
	@Resource(name="basicBomInfoService")
	private IBasicBomInfoService basicBomInfoService;
	
	@Resource(name="basicBomSubstituteService")
	private IBasicBomSubstituteService basicBomSubstituteService;
	
	@Resource(name="syCodeService")
	private SyCodeService syCodeService;
	
//	@Resource(name="basicMaterialsService")
//	private IBasicMaterialsService basicMaterialsService;
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		// TODO Auto-generated method stub
		return MANAGER;
	}

	@RequestMapping(value = "/itemmanager")
	public String itemmanager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("componentSequenceId", request.getParameter("componentSequenceId"));
		return ITEM;
	}
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		String customerId = hander.getQueryValue("customerId");//客户简码
		String packageType = hander.getQueryValue("packageType"); //封装类型
		String spec = hander.getQueryValue("spec");//规格型号
		String code = hander.getQueryValue("code");//成品编码
		Map<String, Object> map = new HashMap<String, Object>();
		if(!"".equals(customerId)){
			map.put("customerId", customerId);
		}
		if(!"".equals(packageType)){
			map.put("packageType", packageType);
		}
		if(!"".equals(spec)){
			map.put("spec", spec);
		}
		if(!"".equals(code)){
			map.put("code", code);
		}
		SearchResult rs=new SearchResult();
		List<BasicMaterialsDO> basicMaterialsDOs = basicBomInfoService.findCpByExample(map);
		rs.setRecordCount(basicMaterialsDOs.size());
		rs.setResults(basicMaterialsDOs);
		OutJsonGrid out=new OutJsonGrid(rs);
		out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		//id为成品的编码code
		List<BasicBomRawMaterialDO> bRawMaterialDOs =	basicBomInfoService.findRawMaterialByExample(id);
		Map<String, Object> map = new HashMap<String, Object>();
		if(!"".equals(id)){
			map.put("assemblyItem", id);
		}
		List<BasicMaterialsDO> bMaterialsDOs = basicBomInfoService.findCpByExample(map);
		model.addAttribute("bRawMaterialDOs",bRawMaterialDOs);
			model.addAttribute("bMaterialsDOs",bMaterialsDOs.get(0));
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicBomInfoDO curAP, BindingResult result, Model model)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	
	/*@RequestMapping(value = "/tree")
	public void treelist(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String tr=null;
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);	
		List<BasicMaterialsDO> list =basicBomInfoService.findCpByExample(null);
		tr=tree(list);
		AjaxUtil.printJSonArr(response, tr);
	}*/
	
    /**
     * 自调函数 生成tree数据
     **/
	/*private String  tree(List<BasicMaterialsDO> list) throws SyBaseException {
		StringBuffer sf =new StringBuffer();
		sf.append("[");
		for(int i=0;i<list.size();i++){
			sf.append("{");
			sf.append("\"text\":").append("\"").append(list.get(i).getCode()).append("\"").append(",").append("\"code\":").append("\"").append(list.get(i).getCode()).append("\"");
			if(i==(list.size()-1)){
				sf.append("}");
			}else{
				sf.append("},");
			}
		}
		sf.append("]");
		return sf.toString();
	}*/
	
	
	@RequestMapping(value = "/itemlist")
	public void ltemlist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		String componentSequenceId = request.getParameter("componentSequenceId");
		String code = hander.getQueryValue("code");
		String  name =hander.getQueryValue("name");
		String  spec =hander.getQueryValue("spec");
		Map<String, Object> map = new HashMap<String, Object>();
		if(!"".equals(componentSequenceId)){
			map.put("componentSequenceId", componentSequenceId);
		}
		if(!"".equals(code)){
			map.put("code", code);
		}
		if(!"".equals(name)){
			map.put("name", name);
		}
		if(!"".equals(spec)){
			map.put("spec", spec);
		}
		List<BasicBomRawMaterialDO> lMaterialsDOs = basicBomInfoService.findSubstituteByExample(map);
		SearchResult result = new SearchResult();
		result.setResults(lMaterialsDOs);
		result.setRecordCount(lMaterialsDOs.size());
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	@RequestMapping("/exportExcel")
	public void exportAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<BasicMaterialsDO> productlist = null;
		List<BasicBomDetailsDO> bomlist = new ArrayList<BasicBomDetailsDO>();
		JqGridHandler hander=new JqGridHandler(request);
		hander.setFilters(new String(request.getParameter("filters").getBytes(
				"ISO-8859-1"), "UTF-8")); // 转码
		String customerId = hander.getQueryValue("customerId");//客户简码
		String packageType = hander.getQueryValue("packageType"); //封装类型
		String spec = hander.getQueryValue("spec");//规格型号
		String code = hander.getQueryValue("code");//成品编码
		Map<String, Object> map = new HashMap<String, Object>();
		if(!"".equals(customerId)){
			map.put("customerId", customerId);
		}
		if(!"".equals(packageType)){
			map.put("packageType", packageType);
		}
		if(!"".equals(spec)){
			map.put("spec", spec);
		}
		if(!"".equals(code)){
			map.put("code", code);
		}
		productlist = basicBomInfoService.findCpByExample(map);
		if(productlist.size()>0){
			for(int i=0;i<productlist.size();i++){
				Map<String, Object> map1 = new HashMap<String, Object>();
				if(!"".equals(productlist.get(i).getCode())){
					map1.put("assemblyItem", productlist.get(i).getCode());
				}
				List<BasicMaterialsDO> bMaterialsDOs = basicBomInfoService.findCpByExample(map1);
				List<BasicBomRawMaterialDO> bRawMaterialDOs =	basicBomInfoService.findRawMaterialByExample(productlist.get(i).getCode());				
				if(bRawMaterialDOs.size()>0){
					for(int j=0;j<bRawMaterialDOs.size();j++ ){
						String componentSequenceId = bRawMaterialDOs.get(j).getComponentSequenceId();
						Map<String, Object> map2 = new HashMap<String, Object>();
						if(!"".equals(componentSequenceId)){
							map2.put("componentSequenceId", componentSequenceId);
						}
						List<BasicBomRawMaterialDO> lMaterialsDOs = basicBomInfoService.findSubstituteByExample(map2);				
						if(lMaterialsDOs.size()>0){
							for(int k=0;k<lMaterialsDOs.size();k++){
								BasicBomDetailsDO bomdetail = new BasicBomDetailsDO();
								if(k==0){
									bomdetail.setProductCode(bMaterialsDOs.get(0).getCode());
									bomdetail.setPackageType(bMaterialsDOs.get(0).getPackageType());
									bomdetail.setProductSpec(bMaterialsDOs.get(0).getSpec());
									bomdetail.setProductDescription(bMaterialsDOs.get(0).getDescription());
									bomdetail.setWaferVersion(bMaterialsDOs.get(0).getWaferVersion());
									bomdetail.setMaterialsNumber(j+1);
									bomdetail.setMaterialsCode(bRawMaterialDOs.get(j).getCode());
									bomdetail.setMaterialsName(bRawMaterialDOs.get(j).getName());
									bomdetail.setMaterialsDescription(bRawMaterialDOs.get(j).getDescription());
									bomdetail.setTotalQty(bRawMaterialDOs.get(j).getTotalQty());
								}
								bomdetail.setSubstituteCode(lMaterialsDOs.get(k).getCode());
								bomdetail.setSubstituteName(lMaterialsDOs.get(k).getName());
								bomdetail.setSubstituteDescription(lMaterialsDOs.get(k).getDescription());
								bomdetail.setSubstituteQty(lMaterialsDOs.get(k).getTotalQty());
								bomlist.add(bomdetail);
							}
						}else{
							BasicBomDetailsDO bomdetail = new BasicBomDetailsDO();
									bomdetail.setProductCode(bMaterialsDOs.get(0).getCode());
									bomdetail.setPackageType(bMaterialsDOs.get(0).getPackageType());
									bomdetail.setProductSpec(bMaterialsDOs.get(0).getSpec());
									bomdetail.setProductDescription(bMaterialsDOs.get(0).getDescription());
									bomdetail.setWaferVersion(bMaterialsDOs.get(0).getWaferVersion());
									bomdetail.setMaterialsNumber(j+1);
									bomdetail.setMaterialsCode(bRawMaterialDOs.get(j).getCode());
									bomdetail.setMaterialsName(bRawMaterialDOs.get(j).getName());
									bomdetail.setMaterialsDescription(bRawMaterialDOs.get(j).getDescription());
									bomdetail.setTotalQty(bRawMaterialDOs.get(j).getTotalQty());
									bomlist.add(bomdetail);
						}
						
					}
				}
			}
		}
		ExcelUtil.toExcel(bomlist, "产品料号:productCode┋封装型号:packageType┋产品型号:productSpec┋产品版本:waferVersion┋产品描述:productDescription┋序号:materialsNumber┋材料类别:materialsName┋材料料号:materialsCode┋材料描述:materialsDescription┋单位用量:totalQty┋替代料类别:substituteName┋替代料料号:substituteCode┋替代料描述:substituteDescription┋替代料单位用量:substituteQty","BOM明细导出",response);
	}
}
